//
//  WTPerGiftPageView.m
//  66TV
//
//  Created by ben on 16/8/8.
//  Copyright © 2016年 shifeng. All rights reserved.
//

#import "WTPerGiftPageView.h"

#import "Masonry.h"
#import "UIScreen+WTExtend.h"

@interface WTPerGiftPageView ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView*  giftCollectionView;
@end

@implementation WTPerGiftPageView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setup];
    }
    return self;
}


- (void)setup{

    self.giftCollectionView = ({

        CGFloat width = [UIScreen screenWidth] / 4;
        UICollectionViewFlowLayout*  layout = [[UICollectionViewFlowLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsZero;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = CGSizeMake(width, 100);
        UICollectionView* view = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        view.dataSource = self;
        view.delegate = self;
        view.backgroundColor = [UIColor clearColor];
        view.scrollEnabled = NO;
        [view registerNib:[UINib nibWithNibName:@"WTPerGiftPageImageCell" bundle:nil] forCellWithReuseIdentifier:@"WTPerGiftPageImageCell"];
        [self addSubview:view];
        view;
    });
    
    [self layout];
}

- (void)layout{
 
    [self.giftCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.and.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark - 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WTPerGiftPageImageCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WTPerGiftPageImageCell" forIndexPath:indexPath];
    WTChatRoomGiftModel* model = self.datas[indexPath.row];
    cell.giftModel = model;
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.datas.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WTChatRoomGiftModel* model = self.datas[indexPath.row];
    if(self.perGiftSelectedBlock){
        self.perGiftSelectedBlock(model);
    }
}

- (void)refresh{
    
    [UIView performWithoutAnimation:^{
        [self.giftCollectionView reloadData];
    }];
}

- (void)setDatas:(NSArray<WTChatRoomGiftModel *> *)datas{
    _datas = datas;
    [self.giftCollectionView reloadData];

}
@end
